package com.hy.mydesktop.client.component.factory;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.AccordionPanelModelEnum;
import com.hy.mydesktop.client.component.meta.FormPanelModelEnum;
import com.hy.mydesktop.client.component.meta.GridModelEnum;
import com.hy.mydesktop.client.component.meta.TextFieldModelEnum;
import com.hy.mydesktop.client.component.meta.WindowModelEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
import com.hy.mydesktop.shared.rpc.util.ComponentTypeEnumConverter;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-11-10；时间：下午02:18:13</li>
 * <li>类型名称：AbstractComponetFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class AbstractComponetFactory {
	
	
	public static final String className = "com.hy.mydesktop.client.component.factory.AbstractComponetFactory";

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建Window控件</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午03:05:09</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	public static <T>T configureAbstractComponet(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		return (T)configureComponent(component,gxtComponentMetaNodeModel);
	}
	
	public static GxtComponentMetaNodeModel removeBaseGxtComponentId(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){

		//设置基础控件的Id
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID)!=null){
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.COMPONENT_ID,null);
			
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.ID,null);
		
		}
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID)!=null){
			//设置基础控件的Id
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.ID,null);
			
			gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.COMPONENT_ID,null);
			
		}
		
		return gxtComponentMetaNodeModel;
	}
	
	public static Component configureBaseComponentId(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){


		//设置基础控件的Id
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID)!=null){
			String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID);
			
			//component.setId(id.replace('.', '_'));
			component.setData(ComponentMetaDataConstants.ID,id);
			component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
		
		}else {
			
			if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID)!=null){
				//设置基础控件的Id
				String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID);
				;
				//component.setId(id.replace('.', '_'));
				component.setData(ComponentMetaDataConstants.ID,id);
				component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
				
			}else {
				MyLoggerUtil.printDebug(className, "没有为当前控件"+component.getClass().getName()+"，设置控件ID");
			}
		}
		
		return component;
	}
	
	private static Component configureBaseComponent(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){


		
		//设置基础控件的Id
		configureBaseComponentId(component,gxtComponentMetaNodeModel);
		
		//设置基础控件的宽度
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.WIDTH)!=null){
			//设置基础控件的宽度
			Object width =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.WIDTH);
			if (width instanceof Integer) {
				if (((Integer)width)<=0) {
					gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.WIDTH, null);
				} else {

					component.setWidth(String.valueOf(((Integer)width)));
				}
			} else {
				component.setWidth(((String)(width)));

			}
		
		}
		
		
		/*if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.INITIALWIDTH.name().toLowerCase())!=null){
			//设置基础控件的初始化宽度
			component.setInitialWidth((Integer)gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.INITIALWIDTH.name().toLowerCase()));
		
		}*/
		
		//设置基础控件的高度
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.HEIGHT)!=null){
			//设置基础控件的高度
			//component.setHeight((String)gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.HEIGHT.name().toLowerCase()));
		
			Object height =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.HEIGHT);
			if (height instanceof Integer) {
				if (((Integer)height)<=0) {
					gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.HEIGHT, null);

				} else {

					component.setHeight(String.valueOf(((Integer)height)));
				}
			} else {
				component.setHeight(((String)(height)));

			}
		}
		
		 //设置高度
		if (gxtComponentMetaNodeModel.get(FormPanelModelEnum.HIGHT.name()
				.toLowerCase()) != null) {
			Object height = (Object) gxtComponentMetaNodeModel
					.get(FormPanelModelEnum.HIGHT.name().toLowerCase());
			if (height instanceof Integer) {
				component.setHeight(height.toString());

			} else {
				component.setHeight(height.toString());
				//component.setHeight(String.valueOf(((Integer)height)));
			}

		}
		//设置基础控件是否显示边框
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.BORDER.name().toLowerCase())!=null){
			//设置基础控件是否显示边框
			Object border =  gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.BORDER.name().toLowerCase());
			if (border instanceof Boolean) {				
				component.setBorders((Boolean)border);
			} else {		
				if ("true".equals(border.toString())) {					
					component.setBorders(true);
				} else {		
					component.setBorders(false);
				}
			}
		}
		
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.TITLE)!=null){
			//设置基础控件标题
			component.setTitle((String)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.TITLE));
		}
		
		
		
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.SEAMCOMPONETNAME.name().toLowerCase())!=null){
			//设置Gwt控件对应的Seam组件的名称
			component.setData("seamComponetName",gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.SEAMCOMPONETNAME.name().toLowerCase()));
		
		}
		//String seamComponetName = (String)field.getData("seamComponetName");

		
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.EVENTSTRUCTUREMETAMODELFORDISPACHERS.name().toLowerCase())!=null){
			//设置基础控件用于转发的，事件结构元模型（后台）
			component.setData(AbstractComponetModelEnum.EVENTSTRUCTUREMETAMODELFORDISPACHERS.name().toLowerCase(),
					gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.EVENTSTRUCTUREMETAMODELFORDISPACHERS.name().toLowerCase()));
			

		}
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.DISPACHEREVENTTYPES.name().toLowerCase())!=null){
			//设置基础控件用于转发的，事件结构元模型（前台）
			component.setData(AbstractComponetModelEnum.DISPACHEREVENTTYPES.name().toLowerCase(),
					gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.DISPACHEREVENTTYPES.name().toLowerCase()));
			
		}
		
		return component;
	}
	/**
	 * 
	 * <ul>
	 * <li>方法含义：对窗口的属性进行配置</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-29；时间：下午下午03:56:36</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static Component configureComponent(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		configureBaseComponent(component, gxtComponentMetaNodeModel);
		configureChildrenItems(component, gxtComponentMetaNodeModel);
		
		
		configureGxtToSeamServiceParameterModel(component, gxtComponentMetaNodeModel);
		configureGxtToSeamServiceParameterModelForMerge(component, gxtComponentMetaNodeModel);
		configureGxtToSeamServiceParameterModelForUpdate(component, gxtComponentMetaNodeModel);
		configureGxtToSeamServiceParameterModelForAdd(component, gxtComponentMetaNodeModel);
		configureGxtToSeamServiceParameterModelForQuery(component, gxtComponentMetaNodeModel);
		
		
		
		
		return component;
	}
	
	
	
	
	
	private static Component configureChildrenItems(Component component,
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			//设置基础控件标题
			List<ItemOfAssociateToWindow> childrenItems = gxtComponentMetaNodeModel.getChildrenItems();
			component.setData(ComponentMetaDataConstants.CHILDREN_ITEMS, childrenItems);
		}
		return component;		
		
	}

	public static Component configureGxtToSeamServiceParameterModel(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODEL.name().toLowerCase())!=null){
			
			BaseModel baseModel = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODEL.name().toLowerCase());
			//baseModel.get("serviceComponentName");
			//baseModel.get("serviceMethodName");
			//baseModel.get("methodArguments") ;
			
			GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
			gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
//			;
			ModelData[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
			
			component.setData("gxtToSeamServiceParameter", gxtToSeamServiceParameter);
			component.setData("dataload", gxtToSeamServiceParameter);
			
		}
		
		
		return component;

	}
	
	
	
public static Component configureGxtToSeamServiceParameterModelForUpdate(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载gxttoseamserviceparameterforadd gxttoseamserviceparameterforupdate
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORUPDATE.name().toLowerCase())!=null){
			
			BaseModel baseModel = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORUPDATE.name().toLowerCase());
			//baseModel.get("serviceComponentName");
			//baseModel.get("serviceMethodName");
			//baseModel.get("methodArguments") ;
			
			GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
			gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
//			;
			ModelData[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
			
			component.setData("gxtToSeamServiceParameterForUpdate", gxtToSeamServiceParameter);
			
		}
		
		
		return component;

	}
	


	
	public static Component configureGxtToSeamServiceParameterModelForMerge(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORMERGE.name().toLowerCase())!=null){
			
			BaseModel baseModel = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORMERGE.name().toLowerCase());
			//baseModel.get("serviceComponentName");
			//baseModel.get("serviceMethodName");
			//baseModel.get("methodArguments") ;
			
			GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
			gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
	//		;
			ModelData[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
			
			component.setData("gxtToSeamServiceParameterForMerge", gxtToSeamServiceParameter);
			
		}
		
		
		return component;
	
	}


	public static Component configureGxtToSeamServiceParameterModelForAdd(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORADD.name().toLowerCase())!=null){
			
			BaseModel baseModel = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORADD.name().toLowerCase());
			//baseModel.get("serviceComponentName");
			//baseModel.get("serviceMethodName");
			//baseModel.get("methodArguments") ;
			
			GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
			gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
//			;
			ModelData[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
			
			component.setData("gxtToSeamServiceParameterForAdd", gxtToSeamServiceParameter);
			
		}
		
		
		return component;

	}

	public static Component configureGxtToSeamServiceParameterModelForQuery(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORQUERY.name().toLowerCase())!=null){
			
			BaseModel baseModel = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELFORQUERY.name().toLowerCase());
			//baseModel.get("serviceComponentName");
			//baseModel.get("serviceMethodName");
			//baseModel.get("methodArguments") ;
			
			GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
			gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
//			;
			ModelData[] methodArguments2 = new BaseModel[1];
			methodArguments2[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
			
			component.setData("gxtToSeamServiceParameterForQuery", gxtToSeamServiceParameter);
			
		}
		
		
		return component;

	}
	

	public static Component configureGxtToSeamServiceParameterModels(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//数据加载
		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELS.name().toLowerCase())!=null){
			
			List<GxtToSeamServiceParameter> gxtToSeamServiceParameters = new LinkedList<GxtToSeamServiceParameter>();
			List<BaseModel> baseModels = gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.GXTTOSEAMSERVICEPARAMETERMODELS.name().toLowerCase());
			for (BaseModel baseModel : baseModels) {
				
				GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
				gxtToSeamServiceParameter.setServiceComponentName((String)(baseModel.get("serviceComponentName")));
				gxtToSeamServiceParameter.setServiceMethodName((String)(baseModel.get("serviceMethodName")));
//				;
				ModelData[] methodArguments2 = new BaseModel[1];
				methodArguments2[0] = new BaseModel();
				gxtToSeamServiceParameter.setMethodArguments((BaseModel[])(baseModel.get("methodArguments")) );
				
				gxtToSeamServiceParameters.add(gxtToSeamServiceParameter);
			}
			
			component.setData("gxtToSeamServiceParameters", gxtToSeamServiceParameters);
			
		}
		
		
		return component;

	}
	


	
	
}
